package com.demo.task.ext

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

suspend fun executeMain(
    block: suspend CoroutineScope.() -> Unit
) {
    withContext(Dispatchers.Main) {
        block.invoke(this)
    }
}

suspend fun executeIO(
    block: suspend CoroutineScope.() -> Unit
) {
    withContext(Dispatchers.Main) {
        block.invoke(this)
    }
}

fun CoroutineScope.launchIO(
    block: suspend CoroutineScope.() -> Unit
) = launch(Dispatchers.IO) {
    block.invoke(this)
}

fun CoroutineScope.launchMain(
    block: suspend CoroutineScope.() -> Unit
) = launch(Dispatchers.Main) {
    block.invoke(this)
}

fun ViewModel.launchIO(
    block: suspend CoroutineScope.() -> Unit
) = viewModelScope.launch(Dispatchers.IO) {
    block.invoke(this)
}

fun ViewModel.launchDefault(
    block: suspend CoroutineScope.() -> Unit
) = viewModelScope.launch(Dispatchers.Default) {
    block.invoke(this)
}
fun ViewModel.launchMain(
    block: suspend CoroutineScope.() -> Unit
) = viewModelScope.launch(Dispatchers.Main) {
    block.invoke(this)
}